package org.creavi.engine.effect;

import java.io.IOException;
import java.net.URL;

import org.creavi.engine.resource.ResourceLocator;
import org.creavi.engine.texture.TextureFactory;

import com.jme.scene.Spatial;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;

public class ParticleFactory {
	private static ParticleFactory particleFactory = new ParticleFactory();
	
	public static ParticleFactory getInstance() {
		return particleFactory;
	}
	
	private ParticleFactory () {
		
	}
	
	public Spatial loadParticleMesh(String name, String texture) {
		Spatial mesh = null;
		URL url = ResourceLocator.locateBinaryParticle(name);
		if (url != null) {
			try {
				mesh = (Spatial)BinaryImporter.getInstance().load(url);
				
				TextureState ts = TextureFactory.getInstance().createTextureState(texture);
				mesh.setRenderState(ts);
				ZBufferState zstate = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
				zstate.setEnabled(true);
				//FIXME: Arreglar con jme-2.0
				//zstate.setFunction(ZBufferState..CF_LEQUAL);
				zstate.setWritable(false);
				mesh.setRenderState(zstate);
				mesh.updateRenderState();
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
		
		return mesh;
	}
}
